package com.smartrm.globalinfrcore.cmd;

import com.smartrm.globalinfrcore.cmd.interceptor.CommandInterceptor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/*
 * @program: common-starters
 * @description: 默认命令执行器
 * @author: houhong
 * @create: 2023-01-11 21:26
 */
@Component
@Scope("requst")
public class DefaultCommandExecutor implements CommandExecutor {


    private final CommandInterceptor first;

    private final CommandConfig commandConfig;

    public AbstractCommandContext commandContext;


    public DefaultCommandExecutor(@Qualifier("commandContextInterceptor") CommandInterceptor first,
                                  CommandConfig commandConfig) {
        this.first = first;
        this.commandConfig = commandConfig;
    }

    @Override
    public <T> T execute(CommandConfig config, Command<T> command) throws Exception {
        return first.execute(this.commandContext, config, command);
    }

    @Override
    public <T> T execute(Command<T> command) throws Exception {

        return first.execute(this.commandContext, commandConfig, command);
    }


    /*
     * 在执行器里面设置命令上下文
     */
    public CommandConfig getCommandConfig() {
        return commandConfig;
    }

    @Override
    public AbstractCommandContext getCommandContext() {
        return this.commandContext;
    }

    @Override
    public void setCommandContext(AbstractCommandContext commandContext) {
        this.commandContext = commandContext;
    }
}